{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Extracting All Aspects and Concepts from Document"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Extracting All Aspects and Concepts from Document\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM, StringConcept\n",
        "\n",
        "\n",
        "# Sample text content\n",
        "text_content = \"\"\"\n",
        "John Smith is a 30-year-old software engineer working at TechCorp. \n",
        "He has 5 years of experience in Python development and leads a team of 8 developers.\n",
        "His annual salary is $95,000 and he graduated from MIT with a Computer Science degree.\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document object from text\n",
        "doc = Document(raw_text=text_content)\n",
        "\n",
        "# Define aspects and concepts directly on the document\n",
        "doc.aspects = [\n",
        "    Aspect(\n",
        "        name=\"Professional Information\",\n",
        "        description=\"Information about the person's career, job, and work experience\",\n",
        "    )\n",
        "]\n",
        "\n",
        "doc.concepts = [\n",
        "    StringConcept(\n",
        "        name=\"Person name\",\n",
        "        description=\"Full name of the person\",\n",
        "    )\n",
        "]\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract all aspects and concepts from the document\n",
        "processed_doc = llm.extract_all(doc)\n",
        "\n",
        "# Access extracted aspect information\n",
        "aspect = processed_doc.aspects[0]\n",
        "print(f\"Aspect: {aspect.name}\")\n",
        "print(f\"Extracted items: {[item.value for item in aspect.extracted_items]}\")\n",
        "\n",
        "# Access extracted concept information\n",
        "concept = processed_doc.concepts[0]\n",
        "print(f\"Concept: {concept.name}\")\n",
        "print(f\"Extracted value: {concept.extracted_items[0].value}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}